/*
 * This object parses a Settings file for Tetris.
 * It is intended to eventually be a general purpose parser.
 */
/*
  Variables are case insensitive
CurrentLevel := 5;
ColorOn := YES;
ShowNextPiece := YES;
BlockImageList[0] := "/Users/MonoBlock1";
BlockImageList[1] := "MonoBlock2";
BlockImageList[2] := "MonoBlock3";
BlockImageList[3] := "MonoBlock4";
*/

char *typesString = {"integer", "char", "float", "bool", "string", "list"};

#import "Parser.h"

@implementation Parser

- init
{
	[super init];
	symbolTable = malloc(sizeof(symbolEntry) * 100);
	if (symbolTable == NULL) {
		fprintf(stderr, "Couldn't allocate memory for symbol table\n");
	}
	lastSymbolNum = 0;

	return self;
}

- dumpSymbolTable
{
	int i;
	symbolEntry *s;

	printf("name   link   type  value  valuePtr  level\n");
 
	for (i=0; i<lastSymbolNum; i++) {
		s = symbolTable[i];
		printf("%s", s->name);
		printf("%d", s->link);
		printf("%s", s->typesString[i]);
		printf("%d", s->value);
		printf("%d", s->valuePtr);
		printf("%d", s->level);
	}
	return self;
}

- (symbolEntry *) allocateSymbol
{
	symbolEntry *tmp; 
	tmp = (symbolEntry *) malloc(sizeof(symbolEntry));
	if (tmp == NULL) {
		fprintf(stderr, "Couldn't allocate enough memory for a new symbol\n");
		fprintf(stderr, "There are currently %d entries in the symbol table\n",
				  lastSymbolNum - 1);
		exit(0);
	}
	return tmp; 
}


- (int) addBoolSymbol:(char *) name :(BOOL) value
{
	int i = lastSymbolNum++;

	symbolTable[i] = [self allocateSymbol];

	symbolTable[i].name = (char *)malloc(strlen(name) + 1);
	strcpy(symbolTable[i]->name, name);
	symbolTable[i].link = lastSymbolNum; // Builtins always ref. next cell.
	symbolTable[i].type = BOOLEAN;
	symbolTable[i].level = 0;
	symbolTable[i]-> value = (int)value;

	return symbol;
}

- (int) addIntSymbol:(char *) name :(int) value
{
	int i = lastSymbolNum++;

	symbolTable[i] = [self allocateSymbol];

	symbolTable[i].name = (char *)malloc(strlen(name) + 1);
	strcpy(symbolTable[i]->name, name);
	symbolTable[i].link = lastSymbolNum; // Builtins always ref. next cell.
	symbolTable[i].type = INTEGER;
	symbolTable[i].level = 0;
	symbolTable[i].value = value;

	return symbol;
}

- parseList:(char) value :(char *) str
{
	if (str[0] != '['
	return self;
}

- (int) addListSymbol:(char *) name :(char *) value
{
	int i = lastSymbolNum++;

	symbolTable[i] = [self allocateSymbol];

	symbolTable[i]->name = (char *)malloc(strlen(name) + 1);
	strcpy(symbolTable[i]->name, name);
	symbolTable[i].link = lastSymbolNum; // Builtins always ref. next cell.
	symbolTable[i].type = LIST;
	symbolTable[i].level = 0;
	symbolTable[i]->value = value;

	return symbol;
}

- addBuiltIns
{
	[self addIntSymbol:"currentLevel" : 5];
	[addBoolSymbol:"colorOn" :YES];
	[addBoolSymbol:"showNextPiece" :YES];
	[addListSymbol:"BlockImageList",  "[MonoBlock1, MonoBlock2, MonoBlock3, MonoBlock4]"] ;

	[self dumpSymbolTable];
	return self;
}


- saveSettings:sender
{

	return self;
}

/*
 * Purpose: Parse a scenerio file.
 * 
 * Attribute value pairs
 */
- parse:(const char *)fileName
{


	return self;
}

- free
{
	free(symbolTable);
	return self;
}
@end
